package dao.impl;
import entity.destine;
import dao.destineDao;
import util.BaseDao;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;

public class destineDaoImpl implements destineDao {
    Connection conn = null;   // 数据库连接
    PreparedStatement pstmt = null;   // 创建PreparedStatement对象
    ResultSet rs = null;   // 创建结果集对象
    public List listByPage(){
        List list = new ArrayList();
        String sql = "select STR_TO_DATE(destine_time,'%Y-%m-%d') as destine_time,id,room_name,destine_name from destineinfo order by destine_time desc";
        try{
            conn = BaseDao.getConn();
            pstmt =conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while(rs.next()){
                destine message = new destine();
                message.setId(rs.getInt("id"));
                message.setRoom_name(rs.getString("room_name"));
                message.setDestine_name(rs.getString("destine_name"));
                message.setDestine_time(rs.getString("destine_time"));
                list.add(message);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeAll(conn, pstmt, rs);
        }
        return list;
    }
    public int save(destine message){
        int result=0;
        String sql="insert into destineinfo(room_name,destine_name,destine_time) values(?,?,?)";
        try {
            conn=BaseDao.getConn();
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,message.getRoom_name());
            pstmt.setString(2,message.getDestine_name());
            pstmt.setString(3,new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            result=pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeAll(conn, pstmt, rs);
        }
        return result;
    }
}